package leetcode.code0123;

/**
 * 207个用例，超时
 * @author CaoYu
 * @date 2021年11月26日
 * @description
 */
public class Solution1 extends Solution123 {

	@Override
	public int maxProfit(int[] prices) {
		// [3,3,5,0,0,3,1,4]
		int len = prices.length;
		int min = Integer.MAX_VALUE;
		int max = 0;
		for (int i = 0; i < len; i++) {
			int price = prices[i];
			max = Math.max(max, price - min + this.f(prices, i + 1, len));
			min = Math.min(min, price);
		}
		return max;
	}

	private int f(int[] prices, int start, int len) {
		int min = Integer.MAX_VALUE;
		int max = 0;
		for (int i = start; i < len; i++) {
			int price = prices[i];
			min = Math.min(min, price);
			max = Math.max(max, price - min);
		}
		return max;
	}

	public static void main(String[] args) {
		Solution1 so = new Solution1();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
	}

}
